package com.suixun.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.CooperationPartnerDisplayMapper;
import com.suixun.dao.redis.RedisDao;
import com.suixun.domain.CooperationPartnerDisplay;
import com.suixun.exceptions.SuixunException;
import com.suixun.service.CooperationPartnerDisplayService;
import com.suixun.utils.ListUtil;

/**
 * <p>
 * 合作伙伴展示 服务实现类
 * </p>
 *
 * @author jacky
 * @since 2018-01-11
 */
@Service
public class CooperationPartnerDisplayServiceImpl
        extends ServiceImpl<CooperationPartnerDisplayMapper, CooperationPartnerDisplay>
        implements CooperationPartnerDisplayService {

    @Resource
    private CooperationPartnerDisplayMapper cooperationPartnerDisplayMapper;

    @Resource
    private RedisDao redisDao;

    @Value("${cooperation.partner.display.num}")
    private Integer displayNum;// 前台展示数量

    @Override
    @Cacheable(value = "CooperationPartnerDisplayCache", key = "'CooperationPartnerDisplay_listOnlyIncludeImgUrl'")
    public List<CooperationPartnerDisplay> listOnlyIncludeImgUrl() {
        Map<String, Object> map = new HashMap<>();
        map.put("displayNum", displayNum);
        return cooperationPartnerDisplayMapper.listOnlyIncludeImgUrl(map);
    }

    @Override
    public PageInfo<CooperationPartnerDisplay> pageOnlyIncludeImgUrl(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(cooperationPartnerDisplayMapper.listOnlyIncludeImgUrl(new HashMap<>()));
    }

    @Override
    @Cacheable(value = "CooperationPartnerDisplayCache", key = "'CooperationPartnerDisplay_listExclusionContent'")
    public List<CooperationPartnerDisplay> listExclusionContent() {
        Map<String, Object> map = new HashMap<>();
        map.put("displayNum", displayNum);
        return cooperationPartnerDisplayMapper.listExclusionContent(map);
    }

    @Override
    @Cacheable(value = "CooperationPartnerDisplayCache", key = "'CooperationPartnerDisplay_getById_' + #id")
    public CooperationPartnerDisplay getById(Integer id) {
        return this.selectById(id);
    }

    @Override
    public void insert(CooperationPartnerDisplay cooperationPartnerDisplay, Boolean isTop) {
        if (isTop != null && isTop) {
            cooperationPartnerDisplay.setTop(String.valueOf(new Date().getTime()));
        }
        this.insert(cooperationPartnerDisplay);
        // 清缓存
        redisDao.flushCaches("CooperationPartnerDisplay*");
    }

    @Override
    public void update(CooperationPartnerDisplay cooperationPartnerDisplay, Boolean isTop) {
        if (cooperationPartnerDisplay.getId() == null) {
            throw new SuixunException("id不能为空");
        }
        Date date = new Date();
        if (isTop != null) {
            if (isTop) {
                cooperationPartnerDisplay.setTop(String.valueOf(date.getTime()));
            } else {
                cooperationPartnerDisplay.setTop("0");
            }
        }

        this.updateById(cooperationPartnerDisplay);
        // 清缓存
        redisDao.flushCaches("CooperationPartnerDisplay*");
    }

    @Override
    public void delete(String ids) {

        this.deleteBatchIds(ListUtil.Integers2List(ids));
        // 清缓存
        redisDao.flushCaches("CooperationPartnerDisplay*");
    }

    @Override
    @Cacheable(value = "CooperationPartnerDisplayCache", key = "'CooperationPartnerDisplay_pageExclusionContent_' + #page + '_' + #size + '_' + #title")
    public PageInfo<CooperationPartnerDisplay> pageExclusionContent(int page, int size, String title, Integer isShow) {
        PageHelper.startPage(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("isShow", isShow);
        return new PageInfo<>(cooperationPartnerDisplayMapper.listExclusionContent(map));
    }

}
